<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../dbcom/MachineCom.php");

$log = new Logger("./../machinectl.log");
$common = new Common();
$ragioncom = new RagionCom();
$machinecom = new MachineCom();

$cont = $_REQUEST['cont'];
switch ($cont) {
    case 1:
        if (!isset($_REQUEST['longitude']) || !isset($_REQUEST['latitude']) || !isset($_REQUEST['des']) || !isset($_REQUEST['communityid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot request"));
            exit();
        }
        $longitude = $_REQUEST['longitude'];
        $latitude = $_REQUEST['latitude'];
        $des = $_REQUEST['des'];
        $communityid = $_REQUEST['communityid'];
        $res = $machinecom->addMechine($communityid, $des, $longitude, $latitude);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success"));
        } else {
            echo json_encode(array("code" => 400, "msg" => "add mechine failed"));
        }
        break;
    case 2:
        if (!isset($_REQUEST['mechineid']) || empty($_REQUEST['mechineid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot request mechineid"));
            exit();
        }
        $mechineid = $_REQUEST['mechineid'];
        $res = $machinecom->deleteMechine($mechineid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success", "data"=>$mechineid));
        }else{
            echo json_encode(array("code" => 400, "msg" => "delete mechine failed"));
        }
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined failed"));
        break;
}
exit();
